<template>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="index" :to="item.path">
      {{ item.meta.title }}
    </el-breadcrumb-item>
  </el-breadcrumb>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()
const breadcrumbs = ref([])

// 生成面包屑导航
const getBreadcrumbs = () => {
  const matched = route.matched.filter(item => item.meta && item.meta.title)
  
  // 首页始终作为第一个面包屑
  const first = matched[0]
  if (first && first.path !== '/') {
    breadcrumbs.value = [
      {
        path: '/',
        meta: { title: '首页' }
      }
    ]
  } else {
    breadcrumbs.value = []
  }
  
  breadcrumbs.value = [...breadcrumbs.value, ...matched]
}

// 监听路由变化，更新面包屑
watch(
  () => route.path,
  () => {
    getBreadcrumbs()
  },
  { immediate: true }
)
</script>

<style scoped>
.el-breadcrumb {
  line-height: 60px;
  font-size: 14px;
}
</style>
